/**
 * @author blancsv@usi.ch
 */

package ch.usi.inf.pf2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

public class PanicSimInfoPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PanicSimMain circuit;
	private PanicSimDrawingArea area;
	private CommonGate selectedGate;

	private JLabel delay;
	private JLabel value;

	private JFrame frame;
	private JTextField textField;
	private int time;

	private int delayValue = 0;

	public PanicSimInfoPanel(final PanicSimMain circuit,
			final PanicSimDrawingArea area) {
		this.setCircuit(circuit);
		this.area = area;

		// Main panel
		JPanel info = new JPanel();
		info.setBorder(new EtchedBorder());
		FlowLayout layout = new FlowLayout();
		info.setLayout(layout);

		// Panel that contains the informations
		JPanel strings = new JPanel();
		GridLayout stringsLayout = new GridLayout(2, 1);
		strings.setBackground(new Color(255, 255, 255));
		strings.setPreferredSize(new Dimension(155, 70));
		strings.setLayout(stringsLayout);

		// Panel that contains the delay button
		JPanel buttons = new JPanel();
		GridLayout buttonsLayout = new GridLayout(2, 1);
		buttons.setBackground(new Color(255, 255, 255));
		buttons.setPreferredSize(new Dimension(30, 70));
		buttons.setLayout(buttonsLayout);

		// Creates the button to change the delay of the gate
		JButton delayButton = new JButton();
		Icon delayIcon = new ImageIcon(getClass().getResource(
				"Icons/icon_stopwatch.png"));
		delayButton.setIcon(delayIcon);
		delayButton.setToolTipText("Change the delay of the selected gate");

		// Values of the informations at the beggining
		delay = new JLabel("Gate Delay : Unknown");
		value = new JLabel("Gate Value : None");

		// Adds an ActionListener for the button
		delayButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
					if (area.getSelectedGate().size()>0
							&& area.getSelectedGate().get(0).getClass() != MegaInput.class) {
						createTimeDialog();
						repaint();

					}
				

			}
		});

		delay.setFont(new Font("Georgia", Font.ROMAN_BASELINE, 14));
		value.setFont(new Font("Georgia", Font.ROMAN_BASELINE, 14));

		// Adds the two panels to the main one
		info.add(strings);
		info.add(buttons);

		// Adds the informations and the button regarding the delay of the gate
		// to the panel
		strings.add(delay);
		buttons.add(delayButton);

		// Adds the informations regarding the value of the gate
		strings.add(value);

		// Sets the background and the size of the main panel
		info.setBackground(new Color(255, 255, 255));
		info.setPreferredSize(new Dimension(200, 80));
		add(info, BorderLayout.CENTER);

		// Adds a Mouse Listener to the Drawing Area
		area.addMouseListener(new MouseAdapter() {
			// If the mouse is pressed on the Drawing Area, it will update the
			// value and delay informations on the panel
			public void mousePressed(final MouseEvent ev) {

				if (area.getSelectedGate().size() == 0 || area.getSelectedGate().size() > 1) {
					delay.setText("Gate Delay : Unknown");
					value.setText("Gate Value : None");
				} else if (area.getSelectedGate().size()>0) {
					if (area.getSelectedGate().get(0).getTimer() == 0) {
						delay.setText("Gate Delay : 1 ms");
					} else {
						delay.setText("Gate Delay : "
								+ area.getSelectedGate().get(0).getTimer() + " ms");
					}
					value.setText("Gate Value : "
							+ area.getSelectedGate().get(0).getGateValue());
				}
			}

		});

	}

	// Update the delay information with the time of the Selected Gate
	public void setLabels() {
		delay.setText("Gate Delay : " + area.getSelectedGate().get(0).getTimer()
				+ " ms");
	}

	public void setCircuit(PanicSimMain circuit) {
		this.circuit = circuit;
	}

	public PanicSimMain getCircuit() {
		return circuit;
	}

	public void setSelectedGate(CommonGate selectedGate) {
		this.selectedGate = selectedGate;
	}

	public CommonGate getSelectedGate() {
		return selectedGate;
	}

	// Creates a new JFrame when the delay button is pressed
	public void createTimeDialog() {

		frame = new JFrame("Waveform Timer");

		Container contentPane = frame.getContentPane();
		contentPane.setLayout(new BorderLayout());

		JPanel panel = new JPanel();
		contentPane.add(panel);

		JLabel timeLabel = new JLabel("Time (ms) :");
		panel.add(timeLabel);

		textField = new JTextField();
		textField.setPreferredSize(new Dimension(200, 30));
		panel.add(textField);

		JButton okButton = new JButton("OK");
		panel.add(okButton);

		JButton cancelButton = new JButton("Cancel");
		panel.add(cancelButton);

		// When cancelButton is pressed, it closes the TimeDialog
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				frame.setVisible(false);
			}
		});

		// When okButton is pressed, it will :
		// 1.pass the content of the TextField to the time instance variable
		// 2.close the TimeDialog
		// 3.set the delay of the gate to the time instance variable
		// 4.call a function that will update the information regarding the
		// delay of the gate
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				String timeString = textField.getText();
				try {
					time = Integer.parseInt(timeString);
					frame.setVisible(false);
					if (time == 0) {
						area.getSelectedGate().get(0).setNewTime(1);
					} else {
						area.getSelectedGate().get(0).setNewTime(time);
					}
					setLabels();
				}
				// If the argument passed to TimeDialog is not a number
				catch (NumberFormatException e) {
					// A new JFrame is created, composed only of a button with
					// an error message
					final JFrame newFrame = new JFrame("Error");

					Container contentPane2 = newFrame.getContentPane();
					contentPane2.setLayout(new BorderLayout());

					JPanel panel = new JPanel();
					contentPane2.add(panel);

					JButton errorButton = new JButton(
							"You should provide some number");

					panel.add(errorButton);

					// If the errorButton is pressed, then the error Frame is
					// closed
					errorButton.addActionListener(new ActionListener() {
						public void actionPerformed(final ActionEvent ev) {
							newFrame.setVisible(false);
						}
					});

					newFrame.setVisible(true);
					newFrame.pack();

				}

			}
		});

		frame.pack();
		frame.setVisible(true);

	}

}
